package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherBankRateMapper;
import com.zjdiepu.www.ibiz.other.IOtherBankRateService;
import com.zjdiepu.www.model.other.dto.OtherBankRate;
import com.zjdiepu.www.model.other.ext.OtherBankRateExt;

/**
 * other_银行利率维护Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年10月25日 14时11分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherBankRateServiceImpl extends BaseService implements IOtherBankRateService {
	public static final Logger logger = LoggerFactory.getLogger(OtherBankRateServiceImpl.class);

	@Autowired
	private OtherBankRateMapper otherBankRateMapper;
	
	@Override
	public void saveUpdateAndDelete(OtherBankRate otherBankRate, OperateType operType) {
		switch (operType) {
		case add:
			otherBankRate.initInsertData(otherBankRate.getModifier(), otherBankRate.getModifierName());
			otherBankRateMapper.insert(otherBankRate);
			break;
		case edit:
			otherBankRate.initUpdateData(otherBankRate.getModifier(), otherBankRate.getModifierName());
			// 影响的记录数
			int updateCount = otherBankRateMapper.updateByPrimaryKeySelective(otherBankRate);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherBankRate.initDeleteData(otherBankRate.getModifier(), otherBankRate.getModifierName());
			// 影响的记录数
			int deleteCount = otherBankRateMapper.updateByPrimaryKeySelective(otherBankRate);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherBankRate> selectByExt(OtherBankRateExt otherBankRateExt) {
		return otherBankRateMapper.selectByExt(otherBankRateExt);
	}
	
	@Override
	public int countByExt(OtherBankRateExt otherBankRateExt){
		return otherBankRateMapper.countByExt(otherBankRateExt);
	}
	
	@Override
	public OtherBankRate selectById(String ID) {
		return otherBankRateMapper.selectByPrimaryKey(ID);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String ID) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherBankRateMapper.clearEmpty(fieldNames, ID);
		}
	}
	
	@Override
	public int countRefined(OtherBankRate otherBankRate) {
		return otherBankRateMapper.countRefined(otherBankRate);
	}

	@Override
	public OtherBankRate selectRefined(OtherBankRate otherBankRate) {
		return otherBankRateMapper.selectRefined(otherBankRate);
	}
	
	@Override
	public int deletedRefined(OtherBankRate otherBankRate) {
		return otherBankRateMapper.deletedRefined(otherBankRate);
	}

	@Override
	public boolean repeat(OtherBankRate otherBankRate) {
		int count = countRefined(otherBankRate);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherBankRate.getId())) {
			return true;
		}
		OtherBankRate dbItem = selectRefined(otherBankRate);
		if(null == dbItem) {
			return false;
		}
		return !otherBankRate.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherBankRateMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherBankRate> otherBankRates) {
		return otherBankRateMapper.insertBatch(otherBankRates);
	}
}
